/**   
* @Title: UsersForeachHandler.java
* @Package gov.pbc.gx.hc.sa.uflo
* @Description: TODO(用一句话描述该文件做什么)
* @author WLF
* @date 2015-11-9 下午4:28:49
* @version V1.0
*/ 
package gov.pbc.nn.kjgl.joinjrfwygl.handle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.ForeachHandler;

import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.joinjrfwygl.dao.JoinJrfwyglDao;
import gov.pbc.nn.kjgl.joinjrfwygl.entity.HandleDept;
import gov.pbc.nn.kjgl.joinjrfwygl.entity.HandleInfo;
import gov.pbc.nn.kjgl.joinjrfwygl.entity.JoinJrfwygl;

/** 
 * @ClassName: UsersForeachHandler
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WLF
 * @date 2015-11-9 下午4:28:49
 * 
 */
@Component("handleUserForeachHandler")
public class HandleUserForeachHandler implements ForeachHandler {

	/** (非 Javadoc) 
	 * Title: handle
	 * Description:
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see com.bstek.uflo.process.handler.ForeachHandler#handle(com.bstek.uflo.model.ProcessInstance, com.bstek.uflo.env.Context)
	 */
	@Resource(name=ProcessClient.BEAN_ID)
	private ProcessClient processClient;

	@Autowired
	private JoinJrfwyglDao joinDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object> handle(ProcessInstance processInstance, Context context) {
		// TODO Auto-generated method stub
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("joinId", processInstance.getBusinessId());
		List<HandleDept>handleDepts= this.joinDao.query("from " +HandleDept.class.getName()+ " where joinId = :joinId and opinion=1 and invalid=false",map);
		ArrayList<Object> businessDeptMaps=new ArrayList<Object>();
		for (HandleDept handleDept : handleDepts) {
			HandleInfo handleInfo=this.joinDao.getHandleInfo(handleDept.getId());
			if(handleInfo!=null)
			{
				Map<String, Object> businessDeptMap=new HashMap<String, Object>();
				businessDeptMap.put("operator",handleInfo.getOperationUsername());	
				List<IUser> users=SecurityUtils.getUserByRoleForDept(handleDept.getDeptId(), Constants.ROLE_CZ);
				List<String> usernames=new ArrayList<String>();
				for (IUser user : users) {
					usernames.add(user.getUsername());
				}
				businessDeptMap.put("manager", usernames);
				businessDeptMaps.add(businessDeptMap);
			}
		}
		return (Collection<Object>) businessDeptMaps;
	}

}
